
package com.catsic.zcjc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.entity.Drawer;
import com.catsic.zcjc.condition.EquipCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.convert.ZcjcConverts;
import com.catsic.zcjc.dao.EquipDao;
import com.catsic.zcjc.entity.Equip;
import com.catsic.zcjc.entity.Station;
import com.catsic.zcjc.entity.System;
import com.catsic.zcjc.exception.ZcjcExceptionCode;
import com.catsic.zcjc.form.EquipForm;
import com.catsic.zcjc.service.EquipService;
import com.catsic.zcjc.service.StationService;
import com.catsic.zcjc.service.SystemService;
import com.catsic.zcjc.specifiction.EquipSpec;


@Service("equipService")
public class EquipServiceImpl extends BaseServiceImpl<Equip, String> implements EquipService{
	
	/**
	 * EquipDao:设备Dao
	 */
	@Autowired
	private EquipDao equipDao;

	@Autowired
	private StationService stationService;
	
	@Autowired
	private SystemService systemService;
	
	@Autowired
	private DrawerService drawerService;

	@Override
	public EquipForm add(EquipForm form) {

		System system = systemService.findById(form.getSystemId());
		Station station = stationService.findById(system.getStation().getId());
		check(form,station);
		
		Equip equip = new Equip();

		equip.setCode(form.getCode());
		equip.setName(form.getName());
		equip.setStation(station);
		equip.setSystem(system);
		equip.setRemark(form.getRemark());
		equip.setType(form.getType());
		equip.setCreateId(form.getCreateId());
		equip.setCreateName(form.getCreateName());
		equip.setMfr(form.getMfr());
		equip.setModel(form.getModel());
		equip.setInstallTime(form.getInstallTime());
		equip.setStatus(form.getStatus());
		equip.setCreateId(form.getCreateId());
		equip.setCreateName(form.getCreateName());
		
		return ZcjcConverts.convert(super.add(equip));
	}
	

	@Override
	public EquipForm update(EquipForm form) {
		
		
		Equip equip = findById(form.getId());
		
		check(form,equip.getStation());
		
		equip.setCode(form.getCode());
		equip.setName(form.getName());
		equip.setRemark(form.getRemark());
		equip.setType(form.getType());
		equip.setMfr(form.getMfr());
		equip.setModel(form.getModel());
		equip.setInstallTime(form.getInstallTime());
		equip.setStatus(form.getStatus());
		
		return ZcjcConverts.convert(super.add(equip));
	}
	

	@Override
	public Equip findById(String id){
		Equip Equip = getById(id);
		if(null == Equip){
			throw new CatsicException(CatsicExceptionCode.实体不存在, ZcjcConstants.EQUIP);
		}
		return Equip;
	}
	

	@Override
	public EquipForm findEquipById(String id){
		Equip Equip = findById(id);
		return ZcjcConverts.convert(Equip);
	}
	

	
	public void check(EquipForm form,Station station) {
		if(hasSameEquip(form,station)){
			throw new CatsicException(ZcjcExceptionCode.设备管理_编码重复);
		}
	}

	public boolean hasSameEquip(EquipForm form,Station station) {
		Equip equip = equipDao.findByCodeAndStation(form.getCode(),station);
		if(form.getId()==null){
			if(equip!=null){
				return true;
			}
		}else{
			if(equip!=null&&!equip.getId().equals(form.getId())){
				return true;
			}
		}
		
		return false;
	}
	
	public Page<EquipForm> findAll(EquipCondition condition, Pageable pageable){
		EquipSpec spec = new EquipSpec(condition);
		Page<Equip> page = equipDao.findAll(spec, pageable);
		List<EquipForm> listForm = new ArrayList<EquipForm>();
		for(Equip equip :page.getContent()){
			listForm.add(ZcjcConverts.convert(equip));
	    }
	    Page<EquipForm> page_ = new PageImpl<EquipForm>(listForm,pageable,page.getTotalElements());
	    return page_;
	}

	
	
	@Override
	public List<EquipForm> query(String stationId) {
		List<Equip> list = this.findByStationId(stationId);
		
		List<EquipForm> listForm = new ArrayList<EquipForm>();
		
		if(CollectionUtils.isNotEmpty(listForm)){
			for(Equip equip :list){
				listForm.add(ZcjcConverts.convert(equip));
		    }
		}
		
		return listForm;
	}


	@Override
	public List<Equip> findByStationId(String stationId) {
		return equipDao.findByStationId(stationId);
	}
	
	@Override
	public void delete(String id){
		Equip equip = findById(id);
	
		super.delete(equip);
	}


	@Override
	public void findFormatDrawer(EquipForm form) {
		Drawer typeD = null;
		if(form.getType()!=null){
			typeD = drawerService.findByParentAndValue(ZcjcConstants.DRAWER_EQUIP_TYPE, form.getType().toString());
			if(typeD!=null){
				form.setCnType(typeD.getName());
			}
		}
	}
}
